package com.xspace.android.xafcommon.base;


import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.view.XListView;

import cn.xspace.app.view.TitleBar;

/**
 * AMapV2地图Base
 */
public abstract class BaseMapActivity extends BaseActivity implements LocationSource {

    protected TitleBar mTitleBar;
    protected AMap aMap;
    protected MapView mapView;
    protected TextView mLocationErrText;
    protected RelativeLayout mRelativeSearchResult;
    protected AutoCompleteTextView mEtMapSearch;
    protected TextView mTvMapSearch;
    protected XListView mXLvMapResult;

    protected AMapLocationClient mLocationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_amap);
        mTitleBar = (TitleBar) findViewById(R.id.map_view_title_bar);
        mapView = (MapView) findViewById(R.id.map_view);
        mLocationErrText = (TextView) findViewById(R.id.location_errInfo_text);
        //搜索界面
        mRelativeSearchResult = (RelativeLayout) findViewById(R.id.relative_search_result);
        mEtMapSearch = (AutoCompleteTextView) findViewById(R.id.et_map_search);
        mTvMapSearch = (TextView) findViewById(R.id.tv_map_search);
        mXLvMapResult = (XListView) findViewById(R.id.xlv_map_result);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        initMap();
        initTitleBar();
        initEventNew();
    }

    protected void showSearchLayout() {
        mRelativeSearchResult.setVisibility(View.VISIBLE);
    }

    protected void hiddenSearchLayout() {
        mRelativeSearchResult.setVisibility(View.GONE);
    }

    /**
     * 设置头像信息
     */
    protected abstract void initTitleBar();

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
        deactivate();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        if (null != mLocationClient) {
            mLocationClient.onDestroy();
        }
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 初始化
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }
    }

    @Override
    public void deactivate() {
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
    }

    /**
     * 设置一些amap的属性
     */
    protected abstract void setUpMap();

    protected void initEventNew() {
        mTitleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}
